package server.business.http;

import io.netty.channel.ChannelHandlerContext;
import io.netty.handler.codec.http.FullHttpResponse;

import java.util.Map;

import server.business.entity.ResultMessage;
import server.business.service.HttpServiceFactory;
import server.business.service.IHttpService;

import com.alibaba.fastjson.JSON;
import com.core.rest.AbstractHttpHandler;

/**
 * 
 *  接口:删除公告
 *  请求url:/notice/delete
 *  参数 :noticeId
 *  返回:void
 *  说明:
 *  @author macky
 *  
 *
 */
public class DeleteNoticeHandler extends AbstractHttpHandler implements Cloneable{

    
    
    @Override
    protected Object clone() throws CloneNotSupportedException {
        // TODO Auto-generated method stub
        return super.clone();
    }

    @Override
    protected void getData(Map<String, String> params, ChannelHandlerContext ctx) {
        // TODO Auto-generated method stub
        
    }

    @Override
    protected void postData(Map<String, String> params, ChannelHandlerContext ctx) {
        // TODO Auto-generated method stub
        
    }

    @Override
    protected void deleteData(Map<String, String> params, ChannelHandlerContext ctx) {
        
        String text = "";
        
        IHttpService service = HttpServiceFactory.getService();
        
        boolean isSuccess = false;
        
        ResultMessage rm = new ResultMessage(true);
        
        if(params == null || params.isEmpty()){
            
            rm.setMessage("please check your params");
            
            rm.setSuccess(false);
            
        } else {
            
            String noticeId = params.get("noticeId");
            
            if(noticeId == null || noticeId.trim().equals("")){
                
                text = "please send an valid noticeId to server";
                
            }
            
            isSuccess = service.deleteNoticeById(noticeId);
        }
        
        
        if(isSuccess){
            
            rm.setMessage("delete notice successful!");
        
        } else {
            
            rm.setMessage("delete notice faild,please check the notice id.");
            
            rm.setSuccess(false);
        }
        
        text = JSON.toJSONString(rm,true);
        
        FullHttpResponse response = wrapContent(text);

        sendResponse(ctx, response);
        
    }

    @Override
    protected void updateData(Map<String, String> params, ChannelHandlerContext ctx) {
        // TODO Auto-generated method stub
        
    }

    @Override
    public AbstractHttpHandler deepCopy() {
        
        AbstractHttpHandler httpHandler = null;
        
        try {
            
            httpHandler = (AbstractHttpHandler)(this.clone());
        
        } catch (CloneNotSupportedException e) {
            
            e.printStackTrace();
        }
        
        return httpHandler;
    }

}
